package com.sky.service.impl;

import com.sky.constant.StatusConstant;
import com.sky.entity.Dish;
import com.sky.entity.Orders;
import com.sky.mapper.DishMapper;
import com.sky.mapper.OrderMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.UserMapper;
import com.sky.service.WorkspaceService;
import com.sky.vo.BusinessDataVO;
import com.sky.vo.DishOverViewVO;
import com.sky.vo.OrderOverViewVO;
import com.sky.vo.SetmealOverViewVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Slf4j
public class WorkspaceServiceImpl implements WorkspaceService {
    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private SetmealMapper setmealMapper;


    /**
     * 查询今日运营数据
     *
     * @return
     */
    @Override
    public BusinessDataVO businessDate() {
        LocalDateTime start = LocalDateTime.now().with(LocalTime.MIN);
        LocalDateTime end = LocalDateTime.now().with(LocalTime.MAX);
        //1、查营业额
        Map map = new HashMap();
        map.put("start", start);
        map.put("end", end);
        map.put("status", Orders.COMPLETED);
        Double turnover = orderMapper.sumForMap(map);

        //2、有效订单数
        Integer validOrderCount = orderMapper.getCountByMap(map);

        map.put("status", null);
        Integer totalorderCount = orderMapper.getCountByMap(map);

        //3、订单完成率
        Double orderCompletionRate = 0D;
        if (totalorderCount != 0) {
            orderCompletionRate = validOrderCount * 1.0 / totalorderCount;
        }

        //4、平均客单价
        Double unitPrice = 0D;
        if (validOrderCount != 0) {
            unitPrice = turnover * 1.0 / validOrderCount;
        }

        //5、新增用户数
        Integer newUsers = userMapper.sumByMap(map);

        return BusinessDataVO.builder()
                .turnover(turnover)
                .validOrderCount(validOrderCount)
                .orderCompletionRate(orderCompletionRate)
                .unitPrice(unitPrice)
                .newUsers(newUsers)
                .build();
    }

    /**
     * 查询订单管理数据
     *
     * @return
     */
    @Override
    public OrderOverViewVO overviewOrders() {
        LocalDateTime start = LocalDateTime.now().with(LocalTime.MIN);
        LocalDateTime end = LocalDateTime.now().with(LocalTime.MAX);
        //1、待接单数量
        Map map = new HashMap();
        map.put("start", start);
        map.put("end", end);
        map.put("status", Orders.TO_BE_CONFIRMED);
        Integer waitingOrders = orderMapper.getCountByMap(map);

        //2、待派送数量
        map.put("status", Orders.CONFIRMED);
        Integer deliveredOrders = orderMapper.getCountByMap(map);

        //3、已完成数量
        map.put("status", Orders.COMPLETED);
        Integer completedOrders = orderMapper.getCountByMap(map);

        //4、已取消数量
        map.put("status", 6);
        Integer cancelledOrders = orderMapper.getCountByMap(map);

        //5、全部订单
        map.put("status", null);
        Integer allOrders = orderMapper.getCountByMap(map);

        return OrderOverViewVO.builder()
                .waitingOrders(waitingOrders)
                .deliveredOrders(deliveredOrders)
                .completedOrders(completedOrders)
                .cancelledOrders(cancelledOrders)
                .allOrders(allOrders)
                .build();
    }

    /**
     * 查询菜品总览
     *
     * @return
     */
    @Override
    public DishOverViewVO overviewDishes() {
        Map map = new HashMap();

        //已起售
        map.put("status", StatusConstant.ENABLE);
        Integer sold = dishMapper.countByMap(map);

        //已停售
        map.put("status", StatusConstant.DISABLE);
        Integer discontinued = dishMapper.countByMap(map);

        return DishOverViewVO.builder()
                .sold(sold)
                .discontinued(discontinued)
                .build();
    }

    /**
     * 查询套餐总览
     *
     * @return
     */
    @Override
    public SetmealOverViewVO overviewSetmeals() {
        Map map = new HashMap();
        //1、已起售
        map.put("status", StatusConstant.ENABLE);
        Integer sold = setmealMapper.countByMap(map);

        //2、已停售
        map.put("status", StatusConstant.DISABLE);
        Integer discontinued = setmealMapper.countByMap(map);


        return SetmealOverViewVO.builder()
                .sold(sold)
                .discontinued(discontinued)
                .build();
    }

    @Override
    public BusinessDataVO getBusinessData(LocalDateTime start, LocalDateTime end) {
        Map map = new HashMap();
        map.put("start", start);
        map.put("end", end);

        //查询总订单数
        Integer totalOrderCount = orderMapper.getCountByMap(map);

        map.put("status", Orders.COMPLETED);
        //营业额
        Double turnover = orderMapper.sumForMap(map);
        turnover = turnover == null ? 0.0 : turnover;

        //有效订单数
        Integer validOrderCount = orderMapper.getCountByMap(map);

        Double unitPrice = 0.0;

        Double orderCompletionRate = 0.0;
        if (totalOrderCount != 0 && validOrderCount != 0) {
            //订单完成率
            orderCompletionRate = validOrderCount.doubleValue() / totalOrderCount;
            //平均客单价
            unitPrice = turnover / validOrderCount;
        }

        //新增用户数
        Integer newUsers = userMapper.sumByMap(map);

        return BusinessDataVO.builder()
                .turnover(turnover)
                .validOrderCount(validOrderCount)
                .orderCompletionRate(orderCompletionRate)
                .unitPrice(unitPrice)
                .newUsers(newUsers)
                .build();
    }

//    /**
//     * 一次性查30天的内容
//     *
//     * @param start
//     * @param end
//     * @return
//     */
//    @Override
//    public List<BusinessDataVO> getBusinessDataList(LocalDate start, LocalDate end) {
//
//        return null;
//    }
}
